<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\RoleModel;
use app\model\NodeModel;
use think\Controller;

class Role extends Base
{
    public $admin_info;
    public function index()
    {
        if (request()->isAjax()) {
            $param = input("param.");
            $where = [];
            $limit = $param['limit'];
            $offset = ($param['page'] - 1) * $limit;
            $role = new RoleModel();
            $user_list = $role->getAllRoles($where, $offset, $limit);
            foreach ($user_list as $key => $vo) {
                // if($vo['role_id'] != '1'){ //判断这个节点是否是超级管理员
                    $user_list[$key]['operate'] = showOperate($this->makeButton($vo['role_id']));
                // }
            }
            $return['count'] = $role->getAllRolesCount($where);  //总数据
            $return['code'] = "0"; //状态码
            $return['data'] = $user_list;
            return json($return);
        }
        return $this->fetch();
    }

    /**
     * 添加角色
     */
    public function addRole()
    {
        if (request()->isPost()) {
            $param = input("post.");
            $role = new RoleModel();
            $flag = $role->insertRole($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        return $this->fetch();
    }

    /**
     * 编辑角色
     */
    public function editRole()
    {
        $role = new RoleModel();
        if(request()->isPost()){
            $param = input("post.");
            $flag = $role->editRoles($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $role_id = input("param.id");
        $role_info = $role->getOneRole(['role_id'=>$role_id]);
        $this->assign([
            'role_info' => $role_info
        ]);
        return $this->fetch();
    }

    /**
     * 删除数据
     */
    public function roleDel()
    {
        $role_id = input("param.role_id");
        $user = new RoleModel();
        $flag = $user->delRole($role_id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 分配权限
    */
    public function distribution()
    {
        $role = new RoleModel();
        if(request()->isPost()){
            $param = input("post.");
            $data['role_id'] = $param['id'];
            $array =   json_decode($param['json'],true);
            $data['rule'] = implode(",",$array);
            $flag = $role->setRoleNode($data);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $role_id = input("param.id");
        $role = new RoleModel();
        $node = new NodeModel();
        $role_info = $role->getOneRole(['role_id'=>$role_id]);
        $role_info['rule'] = explode(',',$role_info['rule']);
        $node_list = $node->getNodeList("1=1");
        $this->admin_info = session('admin_info');
        if ($this->admin_info['role_id'] != 1 ) {
            $parent_role_info = $role->getParentRole(['role_id'=>$role_info['group']]);
            $where['id'] = array('in',$parent_role_info);
            $node_list = $node->getNodeList($where);
            // var_dump($node_list);exit;
        }
        $list = [];
        foreach ($node_list as $k=>$v){
            if($v['pid'] == 0){
                $list[$k]['isParent'] = true; //是否是父级
            }
            $list[$k]['open'] = true;
            $list[$k]['name'] = $v['node_name'];
            $list[$k]['pId'] = $v['pid'];
            $list[$k]['id'] =  $v['id'];
            if(in_array($v['id'],$role_info['rule'])){
                $list[$k]['checked'] = true;
            }
        }

        $this->assign([
            'role_info' => $role_info,
            'json_node' => json_encode(array_values($list))
        ]);
        return $this->fetch('tree');
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        $this->admin_info = session('admin_info');
        if ($id == $this->admin_info['role_id'] || $id == 1) {
            if ($id == 1 && $this->admin_info['role_id'] != 1) {
                return '' ;
            }
            # 超级管理员
             return [
                '编辑' => [
                    'auth' => 'role/editrole',
                    'href' => url('role/editrole', ['id' => $id]),
                    'btnStyle' => 'normal',
                    'icon' => 'fa fa-paste'
                ]
            ];
        }else{
            #普通管理员
            return [
                '编辑' => [
                    'auth' => 'role/editrole',
                    'href' => url('role/editrole', ['id' => $id]),
                    'btnStyle' => 'normal',
                    'icon' => 'fa fa-paste'
                ],
                '删除' => [
                    'auth' => 'role/roledel',
                    'href' => "javascript:roleDel(" .$id .")",
                    'btnStyle' => 'danger',
                    'icon' => 'fa fa-trash-o'
                ],
                '分配权限' => [
                    'auth' => 'user/distribution',
                    'href' => "javascript:distribution(" .$id .")",
                    'btnStyle' => 'danger',
                    'icon' => ''
                ]
            ];
        }
    }

}